﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_AbsenceTypes : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IAbsenceTypeBLL abBLL = new AbsenceTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadDataGrid();
        }
        public void LoadDataGrid()
        {
            IList<AbsenceTypeInfo> infoList = abBLL.ListAbsenceTypes();
            var appPrcs = abBLL.ListApproveProcess();
            foreach(var i in infoList)
            {
                if(i.ApproveProcessSeq == null || i.ApproveProcessSeq == 0)
                {
                    i.ApproveProcessName = appPrcs.First(o => o.Key == 0).Value;
                } else
                {
                    i.ApproveProcessName = appPrcs.First(o => o.Key == i.ApproveProcessSeq).Value;
                }
            }
            RadGrid1.DataSource = infoList;
            RadGrid1.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "DeleteAll")
            {
                GridItem[] items = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem);
                if (items.Length > 0)
                {
                    ((GridCommandItem)items[0]).FireCommandEvent("DeleteSelected", "");
                }
                else
                {
                    RadGrid1.Items[0].FireCommandEvent("DeleteSelected", "");
                }
            }
            else if (e.CommandName == "Delete")
            {
                string Seq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();

                abBLL.Delete(Int32.Parse(Seq), ssMng.EmpSeqI);
            }

            LoadDataGrid();
        }
    }
}
